package com.chenxi.service.impl;

import com.chenxi.mapper.SysAuthorityMapper;
import com.chenxi.model.SysAuthority;
import com.chenxi.service.ISysAuthorityService;
import com.chenxi.util.PageData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author Hu Xiaolong
 * @version V0.1
 * @create 2017/12/12
 */
@Service("sysAuthorityService")
public class SysAuthorityServiceImpl implements ISysAuthorityService{

    @Autowired
    private SysAuthorityMapper sysAuthorityMapper;

    public void test(){

        PageInfo<SysAuthority> pageInfo = PageHelper.startPage(1,10).doSelectPageInfo(()->{
            Example example = new Example(SysAuthority.class);
            Example.Criteria criteria = example.createCriteria();
            Example.OrderBy order = example.orderBy("");
            sysAuthorityMapper.selectByExample(example);
        });
    }


    @Override
    public PageData<SysAuthority> selectPageList (Map<String,String> params){
        PageData<SysAuthority> pageData = new PageData();
        PageInfo<SysAuthority> pageInfo = PageHelper.startPage(1,10).doSelectPageInfo(()->{
            Example example = new Example(SysAuthority.class);
            if (!params.isEmpty()) {
                Example.Criteria criteria = example.createCriteria();
                params.keySet().forEach(key->{
                    criteria.andCondition(key,params.get(key));
                });
            }

            if (params.containsKey("order")) {
                Example.OrderBy order = example.orderBy(params.get("order"));
                if (params.containsKey("sort")&&"desc".equals(params.get("sort"))) {
                    order.desc();
                }else {
                    order.asc();
                }
            }
            sysAuthorityMapper.selectByExample(example);
            //pageData.setData();
        });
        pageData.setRecordsTotal(pageInfo.getTotal());
        pageData.setRecordsFiltered(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        pageData.setDraw(Integer.parseInt(params.get("draw")));
        return pageData;
    }
}
